#!/usr/bin/env python
#-*- coding:utf-8 -*-
from wwpy.wuba.service.IMC import IMC
from wwpy.io.Source import Source
from wwpy.util.Threads import ThreadPool
from wwpy.util.Time import Time
from wwpy.util.Wrapper import looper

class InfoAttr(object):
    
    def __init__(self,date=Time.yesterday(),threads=4):
        self.date=Time.date_format(date)
        self.threads=threads
        self.num=1
        self.imc=IMC()
        
    def get_info_ids(self,idx):
        sql="select distinct entity_id from db_bid_promotion.creative_info_%s where entity_type=0 and date(add_time)='%s' and entity_id!='' and entity_id is not null " %(idx,self.date)
        s=Source(sql)
        info_ids=dict([(row[0],idx) for row in s.read()])
        return info_ids
    
    def get_info(self,info_id):
        try:
            return info_id,self.imc.get_info(info_id)
        except Exception,e:
            print e
            return None
    
    def parse_info(self,info):
        info_id,info=info
        locals=info.getLocals()
        url=info.getUrl()
        params=info.getParams()
        state=info.getState()
        return (info_id,locals,params,url,state)
    
    def do_task(self,info_ids):
        infos=[]
        for info_id in info_ids:
            info=self.get_info(info_id)
            if info is None:
                continue
            parser=self.parse_info(info)
	    print parser
            infos.append(parser)
        return infos
    
    def get_idx_infos(self):
        tp=ThreadPool(self.threads)
        for idx in range(self.num):
            tp.put(self.get_info_ids,idx)
        tp.wait()
        info_ids={}
        for out in tp.outs():
            info_ids.update(out[-1])
        idx_infos={}
        for info_id,idx in info_ids.iteritems():
            idx_infos.setdefault(idx,[]).append(info_id)
        return idx_infos
    
    def run(self):
        idx_infos=self.get_idx_infos()
        self.imc.start()
        results=[]
        for idx,info_ids in idx_infos.iteritems():
            res=self.do_task(info_ids)
            results.extend(res)
        self.imc.stop()
        return results
    
    def do_job(self):
        idx_infos=self.get_idx_infos()
        self.imc.start()
        tp=ThreadPool(self.threads)
        for idx in range(self.num):
            info_ids=idx_infos.get(idx)
            if info_ids is None:
                continue
            tp.put(self.do_task,info_ids)
        tp.wait()
        results=[]
        for out in tp.outs():
            results.extend(out[-1])
        self.imc.stop()
        return results
    
@looper('DAY')
def main(date):
    ia=InfoAttr(date)
    ia.run()
    
if __name__ == '__main__':
    main()
